<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>获取地区轮廓线</title>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script>
    <script type="text/javascript" src="CityData.js"></script>
    <style type="text/css">
        body {
            font-size: 13px;
            margin: 10px
        }
        #container {
            width: 800px;
            height: 500px;
            border: 1px solid gray
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <div id="event_descr"></div>
    <br />输入省、直辖市或县名称：
    <input type="text" id="districtName" style="width:80px" value="桂林市">
    <input type="button" onclick="getBoundary()" value="获取轮廓线">
    <script type="text/javascript">
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        
        var folderName = "C:\\china\\";
        if (!fso.FolderExists(folderName)) fso.CreateFolder(folderName);
        
        var map = new BMap.Map("container");
        map.centerAndZoom(new BMap.Point(116.403765, 39.914850), 5);
        map.addControl(new BMap.NavigationControl({
            type: BMAP_NAVIGATION_CONTROL_SMALL
        }));
        map.enableScrollWheelZoom();
         //直辖市 
        if (cityData.municipalities && cityData.municipalities.length > 0) {
            for (var i = 0, n = cityData.municipalities.length; i < n; i++) {
                var mu = cityData.municipalities[i];
                getBoundary(mu.n, "");
            }
        }
         // 普通省
        for (var i = 0, n = cityData.provinces.length; i < n; i++) {
            var pv = cityData.provinces[i];
            var newFolderName = folderName + pv.n
            if (!fso.FolderExists(newFolderName)) newFolderName = fso.CreateFolder(folderName + pv.n);
            getBoundary(pv.n, "");
            for (var j = 0, m = pv.cities.length; j < m; j++) {
                getBoundary(pv.cities[j].n, pv.n);
            }

        }

         // 其他地区
        if (cityData.other && cityData.other.length > 0) {
            for (var i = 0, n = cityData.other.length; i < n; i++) {
                var oth = cityData.other[i];
                getBoundary(oth.n, "");
            }

        }

        function getBoundary(name, pvName) {
            var bdary = new BMap.Boundary();

            bdary.get(name, function(rs) { //获取行政区域
                //  map.clearOverlays(); //清除地图覆盖物       
                //  var count = rs.boundaries.length; //行政区域的点有多少个
                //  for (var i = 0; i < count; i++) {
                //      var ply = new BMap.Polygon(rs.boundaries[i], {
                //           strokeWeight: 2,
                //           strokeColor: "#ff0000"
                //       }); //建立多边形覆盖物
                //       map.addOverlay(ply); //添加覆盖物
                //     map.setViewport(ply.getPath()); //调整视野         

                //  }
                var fileName = "";
                var newFileObject = null;
                if (pvName == "") newFileObject = fso.CreateTextFile(folderName + name + ".txt", true)
                else newFileObject = fso.CreateTextFile(folderName + pvName + "\\" + name + ".txt", true);

                newFileObject.write(rs.boundaries[0]);
                newFileObject.Close();
                //  eventsTable.innerHTML = rs.boundaries[0].length +':'+ rs.boundaries[0];
            });
        }
    </script>
</body>

</html>



